//
//  DYNetworkDataCell.m
//  PrivatePhoto
//
//  Created by  on 2020/10/17.
//  Copyright © 2020年 . All rights reserved.

#import "DYNetworkDataCell.h"
#import "DYUtility.h"

@implementation DYNetworkDataCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setData:(id)data delegate:(id)delegate {
    if ([data isKindOfClass:[NSDictionary class]]) {
        if ([data[@"data"] isKindOfClass:[TTNetworkDataCellModel class]]) {
            TTNetworkDataCellModel *model = data[@"data"];
            
            //网络延迟
            if (model.networkDelay.length > 0) {
                _networkDelayLabel.attributedText = [DYUtility getNewColorStr:model.networkDelay headFont:[UIFont boldSystemFontOfSize:20] headColor:kUIToneBackgroundColor lastStr:@"ms" lastFont:[UIFont boldSystemFontOfSize:15] lastColor:kUIToneBackgroundColor];
            } else {
                _networkDelayLabel.text = @"- -";
            }
            
            //下载速度
            if (model.downloadSpeed.length > 0) {

                NSString *string = [NSString stringWithFormat:@"%@",kSafeString(model.downloadSpeed)];
                NSArray *array = [string componentsSeparatedByString:@"|"];
                NSString *speed = @"";
                NSString *uni = @"";
                if (array.count > 0) {
                    speed = array[0];
                }
                if (array.count > 1) {
                    uni = array[1];
                }
                
                _downloadSpeedLabel.attributedText = [DYUtility getNewColorStr:speed headFont:[UIFont boldSystemFontOfSize:20] headColor:kUIToneBackgroundColor lastStr:kSafeString(uni) lastFont:[UIFont boldSystemFontOfSize:15] lastColor:kUIToneBackgroundColor];
                
            } else {
                _downloadSpeedLabel.text = @"- -";
            }
            
            //上传速度
            if (model.uploadSpeed.length > 0) {

                NSString *string = [NSString stringWithFormat:@"%@",kSafeString(model.uploadSpeed)];
                NSArray *array = [string componentsSeparatedByString:@"|"];
                NSString *speed = @"";
                NSString *uni = @"";
                if (array.count > 0) {
                    speed = array[0];
                }
                if (array.count > 1) {
                    uni = array[1];
                }
                
                _uploadSpeedLabel.attributedText = [DYUtility getNewColorStr:speed headFont:[UIFont boldSystemFontOfSize:20] headColor:kUIToneBackgroundColor lastStr:kSafeString(uni) lastFont:[UIFont boldSystemFontOfSize:15] lastColor:kUIToneBackgroundColor];
            } else {
                _uploadSpeedLabel.text = @"- -";
            }
        }
    }
}

@end


#pragma mark - Model

@implementation TTNetworkDataCellModel

+ (TTNetworkDataCellModel *)ittemModelWithNetworkDelay:(NSString *)networkDelay downloadSpeed:(NSString *)downloadSpeed uploadSpeed:(NSString *)uploadSpeed {
    TTNetworkDataCellModel *model = [TTNetworkDataCellModel new];
    model.networkDelay = kSafeString(networkDelay);
    model.downloadSpeed = kSafeString(downloadSpeed);
    model.uploadSpeed = kSafeString(uploadSpeed);
    return model;
}

@end
